import numpy as np

def WPA(f, N=50, D=10, k_max=500, alpha=4, beta=6, S=1000, w=500):
    # 1. 初始化
    X = np.random.uniform(low, high, (N, D))
    Y = np.array([f(x) for x in X])
    g = X[np.argmax(Y)].copy()          # 头狼
    for k in range(k_max):
        # 2. 探狼游走
        S_num = np.random.randint(N//(alpha+1), N//alpha)
        for i in np.argsort(-Y)[:S_num]:
            for _ in range(T_max):
                directions = random_directions()
                x_try = X[i] + step_r * directions
                y_try = f(x_try)
                if y_try > Y[i]:
                    X[i], Y[i] = x_try, y_try
                    if y_try > f(g): g = x_try.copy()
                    break
        # 3. 猛狼奔袭 + 4. 围攻
        M_num = N - S_num - 1
        for i in np.argsort(-Y)[S_num+1:]:
            while distance(X[i], g) > d_near:
                X[i] += step_b * (g - X[i]) / distance(X[i], g)
                Y[i] = f(X[i])
                if Y[i] > f(g): g = X[i].copy()
            # 围攻
            x_att = X[i] + step_c * (2*np.random.rand(D)-1)
            if f(x_att) > Y[i]:
                X[i], Y[i] = x_att, f(x_att)
        # 5. 强者生存
        R = np.random.randint(N//(2*beta), N//beta)
        worst = np.argsort(Y)[:R]
        X[worst] = np.random.uniform(low, high, (R, D))
        Y[worst] = [f(x) for x in X[worst]]
    return g, f(g)